package com.yanq.liancar.adapter;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

import com.yanq.liancar.R;
import com.yanq.liancar.bean.CarBean;
import com.yanq.liancar.view.popupwindow.OnPositiveListener;
import com.yanq.liancar.view.recyclerview.RecyclerAdapter;
import com.yanq.liancar.view.recyclerview.RecyclerHolder;
import com.yanq.liancar.view.recyclerview.RecyclerListener;

import java.util.List;

public class CarAdapter extends RecyclerAdapter<CarBean.DataBean> implements RecyclerListener.OnClickListener<RecyclerHolder> {

    private RelativeLayout carRoot;
    private OnPositiveListener onPositiveListener;

    public void setOnPositiveListener(OnPositiveListener onPositiveListener) {
        this.onPositiveListener = onPositiveListener;
    }

    public CarAdapter(Context context, List<CarBean.DataBean> data) {
        super(context, data, R.layout.item_car);
    }

    @Override
    public void convert(RecyclerHolder holder, CarBean.DataBean model) {
        holder.setTvText(R.id.car_name, model.getDriNum());
        holder.setOnClickListener(this, R.id.car_root);
    }

    @Override
    public void onClick(View v, int position, RecyclerHolder holder) {
        CarBean.DataBean dataBean = mData.get(position);
        if (dataBean != null) {
            String driNum = dataBean.getDriNum();
            if (driNum != null && onPositiveListener != null) {
                onPositiveListener.onPositive(driNum);
            }
            if (carRoot != null) {
                carRoot.setBackgroundResource(R.drawable.shape_car_normal);
            }
            carRoot = v.findViewById(R.id.car_root);
            carRoot.setBackgroundResource(R.drawable.shape_car_selected);
        }
    }
}